var $ = require("../../utils/Ajax.js");
Page({
  data: {
    SetStatus: 1,
    TelNum: 60,
    Phone: null,
    Disa: false,
    Code: null,
    pwdVal: null,
    payFocus: true,
    pwdVal2: null,
    payFocus2: false,
  },

  onShow: function(){
    var UserInfor = wx.getStorageSync('UserInfor').mobile;
    if (UserInfor != null) {
      this.setData({
        Phone: UserInfor,
        Disa: true,
      })
    }
  },

  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "设置支付密码"
    }) 
  },

  // 输入手机号码
  SetTel: function(e){
    this.setData({
      Phone: e.detail.value
    })
  },

  // 获取手机验证码
  ObtainCode: function(){
    var Phone = this.data.Phone;
    if (Phone == null || Phone == ''){
      wx.showToast({
        title: "手机号码不能为空",
        icon: 'none',
        duration: 1500,
        mask: true
      })
    }else{
      var that = this;
      $.Ajax("api/sendMsg", "POST", {
        mob: Phone
      }, function (res) {
        if(res.s){
          var TelNumber = Number(that.data.TelNum);
          var TelSet = setInterval(function () {
            --TelNumber;
            if (TelNumber == 0) {
              that.setData({
                TelNum: 60
              })
              clearInterval(TelSet);
            } else {
              that.setData({
                TelNum: TelNumber
              })
            }
          }, 1000)
        }
        wx.showToast({
          title: res.m,
          icon: 'none',
          duration: 1500,
          mask: true
        })
      })
    }
  },

  // 输入验证码
  SetCode: function(e){
    this.setData({
      Code: e.detail.value
    })
  },

  // 点击确定
  SetSubmit: function(e){
    var Status = e.currentTarget.dataset.status;
    this.setData({
      SetStatus: 2
    })
  },
  

  getFocus1: function () {
    this.setData({ 
      payFocus: true 
    });
  },

  getFocus2: function () {
    this.setData({ 
      payFocus2: true 
    });
  },

  inputPwd: function (e) {
    this.setData({ 
      pwdVal: e.detail.value 
    });
    if (e.detail.value.length >= 6) {
      this.setData({ 
        payFocus2: true 
      });
      this.setData({
        SetStatus: 3
      })
    }
  },

  inputPwd2: function (e) {
    this.setData({
      pwdVal2: e.detail.value
    });
    if (e.detail.value.length >= 6) {
      if (this.data.pwdVal == e.detail.value){
        $.Ajax("api/setPayPasswd","POST",{
          mob: this.data.Phone,
          code: this.data.Code,
          payPwd: this.data.pwdVal2,
          comPayPwd: e.detail.value,
        }, function (res) {
          wx.showToast({
            title: res.m,
            icon: 'none',
            duration: 1500,
            mask: true
          })
          if(res.s){
            setTimeout(function(){
              wx.navigateBack({
                delta: 1
              })
            },1500)
          }
        })
      }else{
        wx.showToast({
          title: "两次输入密码不一致",
          icon: 'none',
          duration: 1500,
          mask: true
        })
        this.setData({
          payFocus2: true,
          pwdVal2: null
        });
      }
    }
  }
})